// PIC18F14K50 pulse
// FileName: pulse 14k50



#include <p18f14k50.h>
#include <delays.h>
#include <timers.h>
#include "lib_adc.h"


#pragma config CPUDIV=NOCLKDIV //CPUデバイダ÷1
#pragma config USBDIV = OFF //USBデバイダ÷1
#pragma config FOSC = HS //外部クロック使用
#pragma config PLLEN = ON //PLL×4
#pragma config PCLKEN = OFF
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = OFF,BOREN = OFF
#pragma config WDTEN = OFF
#pragma config MCLRE = OFF
#pragma config HFOFST = OFF,STVREN = OFF,LVP = OFF,BBSIZ = OFF,XINST = OFF
#pragma config CP0 = OFF,CP1 = OFF,CPB = OFF,CPD = OFF
#pragma config WRT0 = OFF,WRT1 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF
#pragma config EBTR0 = OFF,EBTR1 = OFF,EBTRB = OFF
#pragma config BORV = 30
#pragma config WDTPS = 32768


#define Delay_50uS Delay100TCYx(6)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)


unsigned int M,m,N,n,i;


void UserInit(void);

void high(void);
void low(void);


#pragma interrupt high
#pragma interruptlow low
#pragma code H_vect = 0x0008
void H_isr(void){
_asm goto high _endasm
}
#pragma code L_vect = 0x0018
void L_isr(void){
_asm goto low _endasm
}
#pragma code



void high(){
}

void low(){
}



void UserInit(void){
M = 0; N = 0;
PORTCbits.RC0 = 0;
PORTBbits.RB4 = 0;
}



void main(void){

PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
TRISA = 0b00000000;
TRISB = 0b00000000;
TRISC = 0b11000000; // RC6(AN8)、7(AN9) は入力用
ANSEL = 0b00000000;
ANSELH = 0b00000011; // AN8,9 はアナログADC


INTCON3bits.INT1IE = 0;// int1 割り込み 1で有効
INTCON3bits.INT1IP = 0;// 低優先
INTCON3bits.INT1IF = 0;// フラグ

RCONbits.IPEN = 1;// 高優先・低優先 2段階
INTCONbits.GIEH = 0;
INTCONbits.GIEL = 0;


UserInit();


while(1){

PORTCbits.RC0 = 0; // RC0 は 16pin
PORTBbits.RB4 = 0; // RC1 は 15pin

OpenADC(ADC_FOSC_4 &
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_REF_VDD_VDD &
ADC_REF_VDD_VSS,
ADC_CH8 &
ADC_INT_OFF );

ConvertADC();
while(BusyADC());
m=(ADRESH*256) + ADRESL;

OpenADC(ADC_FOSC_4 &
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_REF_VDD_VDD &
ADC_REF_VDD_VSS,
ADC_CH9 &
ADC_INT_OFF );

ConvertADC();
while(BusyADC());
n=(ADRESH*256) + ADRESL;

M = m/50; // 1から20までの数とする
if(M < 2){ // min 1
M = 1;
}
if(M > 20){ // max 20
M = 20;
}

N = n; // 1から1000までの数とする
if(N < 2){
N = 1;
}
if(N > 1000){
N = 1000;
}


PORTCbits.RC0 = 1;
PORTBbits.RB4 = 1;
for(i=0; i<M; i++){
Delay10TCYx(1);
}
PORTBbits.RB4 = 0;

Delay_1mS;
PORTCbits.RC0 = 0;

for(i=0; i<N; i++){
Delay_10mS;
}



}
}





            戻る